package com.briup.cms.service.impl;


import com.briup.cms.bean.Carousel;
import com.briup.cms.dao.CarouselMapper;
import com.briup.cms.dao.extend.CarouselExtendMapper;
import com.briup.cms.exception.CustomerException;
import com.briup.cms.service.ICarouselService;
import com.briup.cms.utils.MessageStatus;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @zhushi
 * @Author kongbai
 * @Date 2021/7/18 11:06
 * @Versiom 1.0
 */
@Service
public class CarouselServiceImpl implements ICarouselService {
    @Autowired
    private CarouselMapper carouselMapper;
    @Autowired
    private CarouselExtendMapper carouselExtendMapper;

    //分页查询所有轮播页
    @Override
    public PageInfo<Carousel> findAll(int pageNum, int pageSize) throws CustomerException {
        PageHelper.startPage(pageNum,pageSize);
        List<Carousel> list = carouselExtendMapper.findAll();
        PageInfo<Carousel> pageInfo = new PageInfo<>(list);
        return  pageInfo;
    }

    //删除轮播页
    @Override
    public void deleteById(int id) throws CustomerException {
        Carousel carousel = carouselMapper.selectByPrimaryKey(id);
        if(carousel ==null){
            throw new CustomerException(MessageStatus.SPECIFIED_QUESTIONED_CAROUSEL_NOT_EXIST);
        }else {
         carouselMapper.deleteByPrimaryKey(id);}

    }

    //查询启用的轮播页
    @Override
    public List<Carousel> findYesCarousel() throws CustomerException {
        List<Carousel> list = carouselExtendMapper.selectByStatus(1);
        if (list ==null){
            list = new ArrayList();
        }
        return (List) list;
    }

    //更新轮播页信息
    @Override
    public void updateCarousel(Carousel carousel) throws CustomerException {
            carouselMapper.updateByPrimaryKeySelective(carousel);
    }

    //添加轮播页信息
    @Override
    public void saveCarousel(Carousel carousel) throws CustomerException {
        carousel.setCarouselStatus(0);
        carouselMapper.insert(carousel);
    }
}
